#ifndef T_GRAPHICS_H
#define T_GRAPHICS_H

#include <QList>
#include <QHash>
#include <QHeaderView>
#include <QMenu>
#include <QGraphicsSceneMouseEvent>
#include <qpoint.h>

#include "T_BasePage.h"
#include "ElaGraphicsItem.h"
#include "ElaGraphicsScene.h"
#include "ElaGraphicsView.h"
#include "T_TreeViewModel.h"
#include "ElaTreeView.h"
#include "ElaText.h"
#include "ElaSlider.h"
#include "ElaPushButton.h"
#include "ElaScrollBar.h"
#include "ElaToggleButton.h"
#include "ElaToggleSwitch.h"
#include "T_CaptionTableViewModel.h"

class CustomGraphicsPixmapItem : public QObject, public QGraphicsPixmapItem {
    Q_OBJECT

public:
    // 添加 cameraId 成员用于标识摄像头
    explicit CustomGraphicsPixmapItem(const QString &cameraId, QGraphicsItem* parent = nullptr)
        : QObject(), QGraphicsPixmapItem(parent), cameraId(cameraId) {
        setAcceptHoverEvents(true);
        setFlag(QGraphicsItem::ItemIsSelectable);  // 允许选中
        setFlag(QGraphicsItem::ItemIsMovable);     // 允许拖动（可选）
        setCursor(Qt::PointingHandCursor);         // 设置鼠标手势
    }

    QString getCameraId() const { return cameraId; }

signals:
    void hovered(bool entered);
    void clicked();
    void requestSwapPosition(const QString &cameraId, QPoint targetPos);  // 新增信号

protected:
    void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override {
        emit hovered(true);
        QGraphicsPixmapItem::hoverEnterEvent(event);
    }

    void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override {
        emit hovered(false);
        QGraphicsPixmapItem::hoverLeaveEvent(event);
    }

    void mousePressEvent(QGraphicsSceneMouseEvent* event) override {
        if (event->button() == Qt::LeftButton) {
            emit clicked();
        } else if (event->button() == Qt::RightButton) {  // 右键触发菜单
            showContextMenu(event->screenPos());          // 使用屏幕坐标
        }
        QGraphicsPixmapItem::mousePressEvent(event);
    }

private:
    QString cameraId;  // 摄像头唯一标识符

    void showContextMenu(const QPoint &screenPos);
};

class WardGraphics : public T_BasePage
{
    Q_OBJECT
public:
    Q_INVOKABLE explicit WardGraphics(QWidget* parent = nullptr);
    ~WardGraphics();

     // 动态布局参数
    void setGridLayout(int rows, int cols);  // 设置布局行列数
    void addCamera(const QString &cameraId); // 动态添加摄像头
    int gridColumns() const { return grid_cols; }
    int gridRows() const { return grid_rows; }
    QSize gridCellSize() const; // 计算每个格子的尺寸

    QGraphicsView* view;
    QGraphicsScene* scene;
    ElaTreeView* _treeView{nullptr};

    void resizeEvent(QResizeEvent* event); // 重写窗口大小变化方法

    int grid_rows = 2;  // 默认2x2布局
    int grid_cols = 2;
private:

    // 动态摄像头管理
    QList<CustomGraphicsPixmapItem*> videoItems;  // 替换 video0~video3
    QHash<QString, CustomGraphicsPixmapItem*> cameraMap; // ID到项的映射
    QHash<QString, QPoint> gridPositions;  // 新增：记录摄像头ID到网格坐标的映射

    // 布局更新
    void updateAllItemsPosition();

private slots:
    void swapCameraPosition(const QString &sourceId, QPoint targetGrid);
    void handleViewResize();

public:

    // TODO: check and clean?
    ElaToggleButton *btn_vid_display;
    ElaToggleButton *btn_vid_save;
    ElaToggleButton *btn_aud_display;
    ElaToggleSwitch *sw_talking_detect;

    T_CaptionTableViewModel* caption_list;

public slots:
    int pic_update(QImage img, int id);
};


#endif // T_GRAPHICS_H
